home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / X-Ray / X-Ray INIT Source / ShowInitIcon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-07  |  8.1 KB  |  269 lines  |  [TEXT/CWIE]

  1. #include <Icons.h>
  2. #include <Resources.h>
  3. #include <Gestalt.h>
  4. #include <Sound.h>
  5. #include <NumberFormatting.h>
  6.  
  7.  
  8. /*
  9.     ShowInitIcon.c
  10.     ShowINIT compatible routine that shows 'iclx' and 'icns' icons.
  11.     For use by all INITs in System 7.0 and beyond.
  12.     Requires OS 7.0 and up.
  13.     
  14.     4/7/99
  15.     Modified by Bruce Partridge to handle Icon Services icns resources.    
  16.     Also fixed for PowerPC data alignment, and removed support for
  17.     pre Color Quickdraw systems.  Now runs either as 68K or PowerPC.
  18.     Send comments/bugs to: Bruce Partridge at <bpart1@aol.com> or <halfhill@kagi.com>
  19.     
  20.  
  21.     This code was written or modified by Paul Mercer, Darin Adler, Paul Snively,
  22.     Steve Capps, Patrick C. Beard, Peter N Lewis, François Pottier, and Jim W Walker.
  23.  
  24.     This code is in the public domain.
  25.  
  26.     Instructions for use:
  27.     
  28.         ••• You must include IconServicesLib to compile this as PowerPC code
  29.         ••• BE SURE TO WEAK LINK THE LIBRARY or you will get an endless and
  30.         ••• baffling series of crashes when Icon Services is not present
  31.         ••• ie OS < 8.5 .
  32.         
  33.         • Create a family of icons with ResEdit 2.1 or later.  This will
  34.             include 'ICN#', 'icl4', & 'icl8' icons, all with the
  35.             same resource ID.
  36.         • Optionally create an icns with the same resource ID for use with
  37.             OS 8.5 and up.  If your INIT is going to display an icon on OS
  38.             < 8.5 you must supply the icon family as well.
  39.         
  40.         To use within a larger INIT project:
  41.             • #define SEPARATE_CODE 0  below.
  42.             • Call ShowInitIcon( id, adv ) with the resource id of
  43.                 the family that you used.  The Boolean parameter adv
  44.                 indicates whether you want the next use of ShowInitIcon
  45.                 to advance to a new position.  Normally you pass TRUE,
  46.                 but you can get an animated-icon effect by passing FALSE
  47.                 all except the last time you call ShowInitIcon.
  48.         
  49.         To use as a separate code resource:
  50.             • #define SEPARATE_CODE 1  below.
  51.             • Set the project type to code resource, set the code type
  52.                 and resource ID (I use type 'Code', ID -4048 for cdevs),
  53.                 compile and merge into your INIT or cdev file.
  54.                 Set the resource type to Locked, so you won't have
  55.                 to call HLock when you use it.
  56.             • In your main INIT code, call ShowInitIcon like so:
  57.                 pascal void (*ShowInitIcon)( short resid, Boolean adv );
  58.                 Handle  show_init;
  59.                 
  60.                 show_init = GetResource( 'Code', -4048 );
  61.                 ShowInitIcon = (pascal void (*)(short, Boolean))
  62.                             StripAddress( *show_init );
  63.                 ShowInitIcon( iconID, advance );
  64.  */
  65.  
  66. #define SEPARATE_CODE    0
  67.  
  68.  
  69. #if SEPARATE_CODE
  70. #define        ShowInitIcon        main
  71. #endif
  72.  
  73. #ifndef nil
  74. #define nil ((void*)0L)
  75. #endif
  76.  
  77. #ifndef topLeft
  78.     #define topLeft(r) ((Point*)&r)[0])
  79. #endif
  80. #ifndef botRight
  81.     #define botRight(r) ((Point*)&r)[1])
  82. #endif
  83.  
  84. // Alignment here must be 68K, or screenbits.bounds address will be off by
  85. // padding and GetIconRect will return incorrect values. BP
  86. #pragma options align=mac68k
  87. typedef struct myQDGlobals {
  88.     char privates[76];
  89.     long randSeed;
  90.     BitMap screenBits;
  91.     Cursor arrow;
  92.     Pattern dkGray;
  93.     Pattern ltGray;
  94.     Pattern gray;
  95.     Pattern black;
  96.     Pattern white;
  97.     GrafPtr thePort;
  98.     long    end;
  99. } myQDGlobals;
  100. #pragma options align=reset
  101.  
  102. /* prototypes */
  103. pascal void ShowInitIcon(short iconId, Boolean advance);
  104. static void Next_position( void );
  105. static void GetIconRect( register Rect* iconRect, Rect *screen_rect );
  106. OSErr GetSelfFSSpec(FSSpec* theFSSpec);
  107.  
  108. /*
  109. //-------------------------------------
  110. main(void)    // For testing purposes only
  111. {
  112.     int iconId = 128;
  113.     unsigned long finalTick;
  114.  
  115.     ShowInitIcon(iconId,true);
  116.     Delay(200,&finalTick);
  117.  
  118.     return (0);
  119. }
  120. //-------------------------------------
  121. */
  122.  
  123. /* this is where it all happens. */
  124. pascal void ShowInitIcon( short iconId, Boolean advance)
  125. {
  126.     long    oldA5;
  127.     myQDGlobals qd;                /* our QD globals. */
  128.     CGrafPort gp;                /* our grafport. */
  129.     Rect    icon_rect;
  130.     OSErr theErr;
  131.     FSSpec theFSSpec;
  132.     IconRef iconRef;
  133.     long gestaltval;
  134.     OSType fakecreator = 'MMac';    // anything will do here
  135.     OSType faketype = 'OS85';        // for Icon Services calls
  136.     
  137.     // Do we have Color QuickDraw etc ?
  138.     Gestalt(gestaltSystemVersion,&gestaltval);
  139.     if(gestaltval < 0x700)
  140.         return;    
  141.  
  142.     /* get a value for A5, a structure that mirrors qd globals. */
  143.     oldA5 = SetA5((long)&qd.end);
  144.     InitGraf(&qd.thePort);
  145.     
  146.     GetIconRect( &icon_rect, &(qd.screenBits.bounds) );
  147.     OpenCPort(&gp);
  148.     
  149.     // test for Icon Services icns handling
  150.     Gestalt(gestaltIconUtilitiesAttr,&gestaltval);
  151.     if(gestaltval & gestaltIconUtilitiesHasIconServices)
  152.     {
  153.         // Icon Services will preferentially load icns over iclx of the same ID
  154.         // If there's no icns, it'll load the iclx
  155.         theErr = GetSelfFSSpec(&theFSSpec);
  156.         if(theErr == noErr)
  157.             theErr = RegisterIconRefFromResource (fakecreator,faketype,&theFSSpec,iconId,&iconRef);
  158.         if(theErr == noErr)
  159.         {
  160.             theErr = PlotIconRef(&icon_rect,kAlignAbsoluteCenter,nil,kIconServicesNormalUsageFlag,iconRef);
  161.             theErr = UnregisterIconRef (fakecreator,faketype);
  162.         }
  163.     }    
  164.     else
  165.         theErr = PlotIconID( &icon_rect, atNone, ttNone, iconId );
  166.     CloseCPort(&gp);
  167.  
  168.     if (advance)
  169.         Next_position(); /* JWW */
  170.  
  171.     SetA5(oldA5);
  172. }
  173.  
  174. /*
  175.     ShowInit's information is nestled at the tail end of CurApName.
  176.     It consists of a short which encodes the next horizontal offset,
  177.     and another short which is that value checksummed with the function below.
  178.  */
  179.  
  180. #define CurApName_LM    0x910
  181. #define ShowINITTable ((unsigned short*)(CurApName_LM + 32 - 4))
  182. #define CheckSumConst 0x1021        /* magic value to check-sum with. */
  183.  
  184. #define InitialXPosition 8            /* initial horizontal offset. */
  185. #define YOffset            40            /* constant from bottom to place the icon. */
  186. #define XOffset            40            /* amount to change it by. */
  187. #define ICON_WIDTH        32
  188. #define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
  189.  
  190. /* CheckSum() computes the magic value to determine if ShowInit's have run already. */
  191.  
  192. static short CheckSum(register unsigned short x)
  193. {
  194.     if (x & 0x8000)    /* high bit set */
  195.         return((x << 1) ^ CheckSumConst ^ 0x01);
  196.     else
  197.         return ((x << 1) ^ CheckSumConst);
  198. }
  199.  
  200. /*
  201.     GetIconRect() generates an appropriate rectangle to display the
  202.     next INIT's icon in.
  203.     It is also responsible for updating the horizontal
  204.     position in low memory.  This is a departure from
  205.     the original ShowInit code, which updates low
  206.     memory AFTER displaying the icon. -- changed by JWW
  207.     This code won't generate an icon position until it is certain that the icon can be loaded,
  208.     so the same behaviour occurs.
  209.     
  210.     This routine also generates a rectangle which is guaranteed to be onscreen.  It
  211.     does this by taking the horizontal offset modulo the screen width to generate
  212.     the horizontal position of the icon, and the offset divided by the screen
  213.     width to generate the proper row.
  214.  */
  215.  
  216. static void GetIconRect(register Rect* iconRect, Rect *screen_rect )
  217. {
  218.     register short screenWidth;
  219.     screenWidth = screen_rect->right - screen_rect->left;
  220.     screenWidth -= screenWidth % XOffset;
  221.     /* if we are the first INIT to run we need to initialize the horizontal value. */
  222.     if (CheckSum(ShowINITTable[0]) != ShowINITTable[1])
  223.         ShowINITTable[0] = InitialXPosition;
  224.     
  225.     /* compute top left of icon's rect. */
  226.     iconRect->left = (ShowINITTable[0] % screenWidth);
  227.     iconRect->top = screen_rect->bottom -
  228.         YOffset * (1 + (ShowINITTable[0] / screenWidth));
  229.     iconRect->right = iconRect->left + 32;
  230.     iconRect->bottom = iconRect->top + 32;
  231.     
  232. }
  233.  
  234. /*
  235.     JWW: In Beard's original version, this was done at the end of
  236.     GetIconRect. That caused incorrect behavior when IconWrap 1.2 was
  237.     used to wrap icons.  Namely, if an INIT using that version of
  238.     ShowIconFamily was the first in a row, then the second icon in that
  239.     row would land on top of it.
  240. */
  241. static void Next_position( void )
  242. {
  243.     /* advance the position for the next icon. */
  244.     ShowINITTable[0] += XOffset;
  245.     
  246.     /* recompute the checksum. */
  247.     ShowINITTable[1] = CheckSum(ShowINITTable[0]);    
  248. }
  249.  
  250.  
  251. OSErr GetSelfFSSpec(FSSpec* theFSSpec)
  252. {
  253.     OSErr        theErr;
  254.     Str255        procname;
  255.     FCBPBRec    fcbPB;
  256.     // The FSSpec is needed to get an IconRef with Icon Services
  257.     // Process Manager isn't loaded yet so use PB calls BP 4/99
  258.     fcbPB.ioNamePtr = procname;
  259.     fcbPB.ioVRefNum = 0;
  260.     fcbPB.ioRefNum = CurResFile();
  261.     fcbPB.ioFCBIndx = 0;
  262.     theErr = PBGetFCBInfoSync(&fcbPB);
  263.     
  264.     if(theErr == noErr)
  265.         theErr = FSMakeFSSpec(fcbPB.ioVRefNum,fcbPB.ioFCBParID,procname,theFSSpec);
  266.     return theErr;
  267. }
  268.  
  269.